import 'package:flutter/material.dart';
import '../../models/home_model.dart';
import '../../network/home_request.dart';
import 'movie_list_item.dart';

const COUNT = 20;

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('首页'),
      ),
      body: HomeBody(),
    );
  }
}

class HomeBody extends StatefulWidget {
  @override
  _HomeBodyState createState() => _HomeBodyState();
}

class _HomeBodyState extends State<HomeBody> {
  HomeRequest homeRequest = HomeRequest();

  int _start = 0;

  List<MovieItem> movies = [];

  @override
  void initState() {
    super.initState();

    getMovieTopList(_start, 20);
  }

  void getMovieTopList(start, count) {
    homeRequest.getMovieTopList(start, count).then((result) {
      setState(() {
        movies.addAll(result);
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemBuilder: (context, index) {
        return MovieListItem(movies[index]);
      },
      itemCount: movies.length,
    );
  }
}
